#include "layout.h"

Layout *layout_new(GooCanvasItem * parent,LAYOUT_TYPE type,int id,const char *name,gpointer sms)
{
	Layout *layout=(Layout *)g_malloc0(sizeof(Layout)+1);
	layout->parent=parent;
	layout->sms=sms;
	layout->type=type;
	layout->id=id;
	layout->name=g_strdup(name);
	layout->needreload = FALSE;
	layout->layout_property.xml_path = NULL;
	layout->layout_property.last_layout_id = -1;
	return layout;
}

void layout_destroy(Layout * layout)
{
	if(layout->inited&& layout->clear_func)
		layout->clear_func(layout,NULL);

	if (layout->type == LAYOUT_TYPE_WIN) {
		if(layout->name)
	    	free(layout->name);
		free(layout);
	} else if (layout->type == LAYOUT_TYPE_ITEM) {
		layout->inited = FALSE;
	}
}

void layout_show(Layout * layout)
{
	if(!layout->inited)
	{
		if(layout->init_func(layout,NULL))
			layout->inited=TRUE;
		else
			return;
	}

	/*if (layout->needreload) {
		layout->clear_func(layout, NULL);
		if(layout->init_func(layout,NULL)) {
			layout->needreload=TRUE;
			layout->inited=TRUE;
		}
	}*/

	layout->visable=TRUE;
	layout->active=TRUE;
	g_object_set(layout->cavas,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);

	if(layout->show_cb_func)
		layout->show_cb_func(layout,NULL);
}

void layout_hide(Layout * layout)
{
	if(!layout->inited)
		return;
	layout->visable=FALSE;
	layout->active=FALSE;
	if (layout->type == LAYOUT_TYPE_WIN) {
		g_object_set(layout->cavas,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	}

	if(layout->hide_cb_func)
		layout->hide_cb_func(layout,NULL);
}

void layout_set_func(Layout *layout,const char *key,gboolean (*func)( Layout * layout,gpointer data))
{
	if(strcmp(key,"init")==0)
		layout->init_func=func;
	else if(strcmp(key,"clear")==0)
		layout->clear_func=func;
	else if(strcmp(key,"key-press")==0)
		layout->key_press_func=func;
	else if(strcmp(key,"show")==0)
		layout->show_cb_func=func;
	else if(strcmp(key,"hide")==0)
		layout->hide_cb_func=func;
	else
		fprintf(stderr,"%s:Can not set func key=%s\n",__FUNCTION__,key);
}

void layout_set_propertie(Layout *layout, const char *key, gpointer propertie)
{
	if(strcmp(key, "xml_path") == 0)
		layout->layout_property.xml_path = (char *) propertie;
	if(strcmp(key, "last_layout_id") == 0)
		layout->layout_property.last_layout_id = *((int *) propertie);
}

void layout_propertie_reset (Layout *layout)
{
	layout->layout_property.xml_path = NULL;
	layout->layout_property.last_layout_id = -1;
}

